package name.jiaosha.air.install.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.constant.DocID;
import name.jiaosha.air.install.dao.IInstallDao;
import name.jiaosha.air.install.domain.Install;
import name.jiaosha.air.install.service.IInstallService;
import name.jiaosha.air.system.dao.IDocIDFactoryDao;

@Service
public class InstallService implements IInstallService {

	@Autowired
	private IInstallDao installDao;

	@Autowired
	private IDocIDFactoryDao docIDFactoryDao;

	@Override
	public void createInstall(Install install) {

		install.setBuyerPinyin(PinyinToolkit.cn2FirstSpell(install.getBuyer()));
		install.setUnitPinyin(PinyinToolkit.cn2FirstSpell(install.getBuyUnit()));
		install.setState(1);
		install.setDocID(docIDFactoryDao.getDocIDByComIdAndType(
				AppConstants.comId, DocID.Install));

		installDao.save(install);

	}

	@Override
	public PageInfo<Install> getInstallListByComId(Integer comId, int index,
			int pageSize, Map<String, Object> params) {

		String jpql = "SELECT p FROM Install p ";

		if (params.get("state") != null) {

			jpql = jpql + " where p.state=?1";
		}

		return installDao.findForPage(jpql, index, pageSize, params.get("state"));
	}

}
